﻿#include "efficencydialog.h"
#include "ui_efficencydialog.h"
#include"taskmanager.h"
//switchfastpoint为1，快速指向不计算为任务
EfficencyDialog::EfficencyDialog(int switchfastpoint,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EfficencyDialog)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("CalEfficency"));
    miswitch=switchfastpoint;
    if(CalEfficiency())
    {
        showEfficiency();
    }
    connect(ui->doubleSpinBox,SIGNAL(editingFinished()),this,SLOT(doublespinbox_valuechange()));
}

void EfficencyDialog::doublespinbox_valuechange()
{

    double totalTime = ui->doubleSpinBox->value();
    showEfficiency(totalTime);
    //0610修改：删除累赘代码，用缺省参数解决
}

//计算指令效率
bool EfficencyDialog::CalEfficiency()
{
    mylist.clear();
    mvctCmds.clear();
    mvctaskItem.clear();

    double time=0.0;
    bool iffind=false;

    mvctCmds=TaskManager::getInstance()->getSortCommand();
    if(mvctCmds.isEmpty())
        return false;
    mvctaskItem=TaskManager::getInstance()->allTasks();
    foreach(TaskItem *pTasks,mvctaskItem)
    {
        if(pTasks==NULL)
            return false;

        QString name=pTasks->getName();
        if(miswitch)
        {
            if(name.contains("FastPointing"))
                continue;
        }
        for(int i=0;i<mylist.size();++i)
        {
            if(mylist.at(i).tsakname.contains(name))
            {
                iffind=true;
                break;
            }
            iffind=false;
        }
        if(iffind==false)
        {
            mymap m1;
            m1.tsakname=name;
            m1.usedtime=time;
            mylist.append(m1);
        }
    }
    //根据任务 名称加载时间
    foreach(CommandItem *pcommand,mvctCmds)
    {
        QString taskname=pcommand->getName().split(" ").at(0);
        for(int i=0;i<mylist.size();++i)
        {
            if(mylist.at(i).tsakname==taskname)
                mylist.at(i).usedtime+=pcommand->getValue(ScriptItem::Time_End)-pcommand->getValue(ScriptItem::Time_Start);
        }
    }
    //根据名称排序，防止图片中数重叠
    qSort(mylist.begin(),mylist.end(),[](mymap my1,mymap my2){return my1.tsakname<my2.tsakname;});
    return true;

}

//显示界面
void EfficencyDialog::showEfficiency(double totalTime)
{
    if(!totalTime)
    {
        ui->doubleSpinBox->value();
        //根据最后一个任务的时间计算总周期
        CommandItem *lastcommand=mvctCmds.last();
        totalTime=lastcommand->getValue(ScriptItem::Time_End);
    }

    ui->doubleSpinBox->setValue(totalTime);
    QPieSeries *series=new QPieSeries;
    series->setHoleSize(0.5);
    QPieSlice *slice=NULL;
     qSort(mylist.begin(),mylist.end(),[](mymap my1,mymap my2){return my1.tsakname<my2.tsakname;});
    //取出mylist中每个 变量，分别计算占用百分比
    for(int i=0;i<mylist.size();++i)
    {
        //防止图中数据有重叠
        if(mylist.at(i).tsakname.contains("s"))
        {
            mylist.push_back(mylist.at(i));
            mylist.removeAt(i);
        }
        double efficency=mylist.at(i).usedtime/totalTime*100;
        slice= series->append(QString("%1 %2%").arg(mylist.at(i).tsakname).arg(efficency),efficency );
        slice->setLabelVisible();
        slice->setLabelColor(QColor(240,210,255));
    }

    //所有任务总占比
    double usetime=0.0;
    for(int i=0;i<mylist.size();++i)
    {
        usetime+=mylist.at(i).usedtime;
    }
    double total=usetime/(totalTime)*100;
    ui->lineEdit->setText(QString("%1").arg(total));

    QChart *chart=new QChart;
    chart->addSeries(series);
    //chart->legend()->setAlignment(Qt::AlignRight);
    chart->legend()->setVisible(false);
    chart->legend()->setFont(QFont());
    //设置背景透明
    chart->setBackgroundVisible(false);
    //设置抗锯齿
    ui->frame->setRenderHint(QPainter::Antialiasing);
    //设置放大缩小
    ui->frame->setRubberBand(QChartView::HorizontalRubberBand);
    ui->frame->setChart(chart);
}

EfficencyDialog::~EfficencyDialog()
{
    delete ui;
}

